package _binary_search;

import java.util.Arrays;

/**
 * 875. 爱吃香蕉的珂珂
 */
public class No875 {
    public int minEatingSpeed(int[] piles, int h) {
        int n = piles.length;
        int ans = Integer.MAX_VALUE;

        int left = 1, right = Arrays.stream(piles).max().orElseThrow();
        while (left <= right) {
            int mid = left + (right - left >> 1);
            long curSum = 0;
            for (int pile : piles) {
                curSum += (int) Math.ceil((double) pile / mid);
            }
            if (curSum > h) left = mid + 1;
            else {
                right = mid - 1;
                ans = Math.min(mid, ans);
            }
        }
        return ans;
    }
}
